﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class ScanEventDAL : BaseDAL<ScanEventDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ScanEvent model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into ScanEvent(");
            strSql.Append("ScanTime,ConsumerId,ScanSource,ShopId,ScanQrContent,Longitude,Latitude,ScanAreaId,UserId,FailScanTime,IsReceive,Ip,Address,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@ScanTime,@ConsumerId,@ScanSource,@ShopId,@ScanQrContent,@Longitude,@Latitude,@ScanAreaId,@UserId,@FailScanTime,@IsReceive,@Ip,@Address,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@ScanTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScanSource", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScanQrContent", SqlDbType.NVarChar,512) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@ScanAreaId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@FailScanTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsReceive", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Ip", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Address", SqlDbType.NVarChar,64) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.ScanTime;
            parameters[1].Value = model.ConsumerId;
            parameters[2].Value = model.ScanSource;
            parameters[3].Value = model.ShopId;
            parameters[4].Value = model.ScanQrContent;
            parameters[5].Value = model.Longitude;
            parameters[6].Value = model.Latitude;
            parameters[7].Value = model.ScanAreaId;
            parameters[8].Value = model.UserId;
            parameters[9].Value = model.FailScanTime;
            parameters[10].Value = model.IsReceive;
            parameters[11].Value = model.Ip;
            parameters[12].Value = model.Address;
            parameters[13].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ScanEvent model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ScanEvent set ");

            strSql.Append(" ScanTime = @ScanTime , ");
            strSql.Append(" ConsumerId = @ConsumerId , ");
            strSql.Append(" ScanSource = @ScanSource , ");
            strSql.Append(" ShopId = @ShopId , ");
            strSql.Append(" ScanQrContent = @ScanQrContent , ");
            strSql.Append(" Longitude = @Longitude , ");
            strSql.Append(" Latitude = @Latitude , ");
            strSql.Append(" ScanAreaId = @ScanAreaId , ");
            strSql.Append(" UserId = @UserId , ");
            strSql.Append(" FailScanTime = @FailScanTime , ");
            strSql.Append(" IsReceive = @IsReceive , ");
            strSql.Append(" Ip = @Ip , ");
            strSql.Append(" Address = @Address , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScanTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScanSource", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ScanQrContent", SqlDbType.NVarChar,512) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@ScanAreaId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@FailScanTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsReceive", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Ip", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Address", SqlDbType.NVarChar,64) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.ScanTime;
            parameters[2].Value = model.ConsumerId;
            parameters[3].Value = model.ScanSource;
            parameters[4].Value = model.ShopId;
            parameters[5].Value = model.ScanQrContent;
            parameters[6].Value = model.Longitude;
            parameters[7].Value = model.Latitude;
            parameters[8].Value = model.ScanAreaId;
            parameters[9].Value = model.UserId;
            parameters[10].Value = model.FailScanTime;
            parameters[11].Value = model.IsReceive;
            parameters[12].Value = model.Ip;
            parameters[13].Value = model.Address;
            parameters[14].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ScanEvent set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ScanEvent set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, ScanTime, ConsumerId, ScanSource, ShopId, ScanQrContent, Longitude, Latitude, ScanAreaId, UserId, FailScanTime, IsReceive, Ip, Address, IsDelete  ");
            strSql.Append("  from ScanEvent ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ScanEvent where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        public DataSet GetList(int scanSource, DateTime starTime, DateTime endTime, string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ScanEvent where IsDelete=0  and ScanSource=@ScanSource ");
            if (!string.IsNullOrEmpty(starTime.ToString()))
            {
                strSql.Append(" and ScanTime>='" + starTime + "'");
            }
            if (!string.IsNullOrEmpty(endTime.ToString()))
            {
                strSql.Append(" and ScanTime<='" + endTime.AddHours(23).AddMinutes(59).AddMilliseconds(59) + "'");
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            SqlParameter[] parameters = {
					new SqlParameter("@ScanSource", SqlDbType.Int,4)
			};
            parameters[0].Value = scanSource;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
        #endregion

        #region 扩展方法

        /// <summary>
        /// 根据 scanQrContent 查询扫码记录次数
        /// </summary>
        /// <remarks>create 2016-06-26</remarks>

        public int GetModelByScanQrContentCount(string scanQrContent, int consumerId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1)  ");
            strSql.Append("  from ScanEvent ");
            strSql.Append(" where scanQrContent=@scanQrContent and ConsumerId=@ConsumerId  and IsDelete=0");
            SqlParameter[] parameters = {
				new SqlParameter("@scanQrContent", SqlDbType.NVarChar),
                new SqlParameter("@ConsumerId", SqlDbType.Int)
			};
            parameters[0].Value = scanQrContent;
            parameters[1].Value = consumerId;
            var num = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return num != null ? int.Parse(num.ToString()) : 0;
        }

        /// <summary>
        /// 获取最后一次扫码记录
        /// </summary>
        /// <remarks>create 2016-06-26</remarks>
        public DataSet GetModelByScanQrContent(string scanQrContent, int consumerId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append("  from ScanEvent ");
            strSql.Append(" where scanQrContent=@scanQrContent and ConsumerId=@ConsumerId  and IsDelete=0 order by ScanTime desc");
            SqlParameter[] parameters = {
				new SqlParameter("@scanQrContent", SqlDbType.NVarChar),
                new SqlParameter("@ConsumerId", SqlDbType.Int)
			};
            parameters[0].Value = scanQrContent;
            parameters[1].Value = consumerId;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        public DataSet GetModelByScanQrContent(int consumerId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from ScanEvent ");
            strSql.Append(" where  ConsumerId=@ConsumerId  and IsDelete=0 order by ScanTime desc");
            SqlParameter[] parameters = {
                new SqlParameter("@ConsumerId", SqlDbType.Int, 4)
			};
            parameters[0].Value = consumerId;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 根据openid查询最后一条抽奖记录
        /// </summary>
        public DataSet GetModelByConsumerId(int consumerId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from ScanEvent ");
            strSql.Append(" where ConsumerId=@ConsumerId  and IsDelete=0 and IsReceive='false' order by ScanTime desc ");
            SqlParameter[] parameters = {
				new SqlParameter("@ConsumerId", SqlDbType.Int, 4)
			};
            parameters[0].Value = consumerId;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }
        #endregion
	}
}
